<?php
class Attach{
	
	public $primaryKey='';
	public $allowFields=array();
	public $table='';
	public $field='';
	public $where='';
	public $order='';
	public $limit='';
	public $group='';
	public $having='';
	
	public function __construct($primaryKey='',$allowFields=array()){
		$this->primaryKey=$primaryKey;
		$this->allowFields=$allowFields;
	}
	public function reset(){
		// $this->table='';
		// $this->field='';
		// $this->where='';
		// $this->order='';
		// $this->limit='';
		// $this->group='';
		// $this->having='';
	}
	public function table($table){
		$this->table=$table;
		return $this;
	}
	public function field($field){
		$this->field=$field;
		return $this;
	}
	public function where($where){
		$this->where=$where;
		return $this;
	}
	public function order($order){
		$this->order=$order;
		return $this;
	}
	public function limit($limit){
		$this->limit=$limit;
		return $this;
	}
	public function group($group){
		$this->group=$group;
		return $this;
	}
	public function having($having){
		$this->having=$having;
		return $this;
	}
	
	public function total($where=''){
		if(!$where) $where=$this->where;
		$where = $where?" WHERE {$where}":'';
		$sql="SELECT COUNT(*) as count FROM {$this->table} {$where}";
		$this->reset();
		return $sql;
	}
	public function select($where=''){
		if(!$where) $where=$this->where;
		$fields = $this->field !='' ?  $this->field : '*';
		$where = $where?" WHERE {$where}":'';
		$order = $this->order != '' ?  " ORDER BY {$this->order} " : '';
		$limit = $this->limit != '' ? " LIMIT {$this->limit} " : '';
		$group = $this->group != '' ? " GROUP BY {$this->group} " : '';
		$having = $this->having != '' ? " HAVING {$this->having} " : '';
		$sql="SELECT {$fields} FROM {$this->table} {$where} {$group} {$having} {$order} {$limit} ";
		$this->reset();
		return $sql;
	}
	public function find($pri=false){
		$fields = $this->field != "" ?  $this->field :  '*';
		if($pri==false){
			$where= $this->where ;
		}else{
			$where="{$this->primaryKey}=:{$this->primaryKey} ";
		}
		$where=$where?" WHERE {$where} ":'';
		$order = $this->order != "" ?  " ORDER BY {$this->order}" : "";
		$sql="SELECT {$fields} FROM {$this->table} {$where} {$order} LIMIT 1";
		$this->reset();
		return $sql;
	}
	public function insert($array){
		$array=$this->remove_arr($array);
		$sql = "INSERT INTO {$this->table}(".implode(',', array_keys($array)).") VALUES (".implode(',', array_values($array)) . ")";
		$this->reset();
		return $sql;
	}
	public function update($array){
		$data=array();
		if(is_array($array)){
			if(array_key_exists($this->primaryKey, $array)){
				unset($array[$this->primaryKey]);	
			}
			$array=$this->remove_arr($array); 
			$s = '';
			foreach ($array as $k=>$v) {
				$s .="{$k}={$v},";
			}
			$s=rtrim($s, ",");
			$setfield=$s;
		}else{
			$setfield=$array;
		}
		$order = $this->order != "" ?  " ORDER BY {$this->order}" : "";
		$limit = $this->limit != "" ? " LIMIT {$this->limit} " : "";
		if($this->where != ""){
			$where=" WHERE {$this->where} ";
			$sql="UPDATE  {$this->table} SET {$setfield} {$where}";
			$sql.=$order.$limit;
		}else{
			$sql="UPDATE {$this->table} SET {$setfield}  WHERE {$this->primaryKey}=:{$this->primaryKey}";
		}
		$this->reset();
		return $sql;
	}
	
	public function delete($where=''){
		if(!$where) $where=$this->where;
		$where = $where?" WHERE {$where}":'';
		$order = $this->order != "" ?  " ORDER BY {$this->order}" : "";
		$limit = $this->limit != "" ? " LIMIT {$this->limit} " : "";
		if($where=="" && $limit==""){
			$where=" where {$this->primaryKey}=:{$this->primaryKey}";
		}
		$sql="DELETE FROM {$this->table} {$where} {$order} {$limit}";
		$this->reset();
		return $sql;
	}
	
	protected function remove_arr($data){
		$arr=array();
		if($this->field && $this->field!='*'){
			$allowFields=explode(',',$this->field);
			foreach($data as $key=>$value){
				if(in_array($key,$allowFields)){
					$arr[$key]=':'.$key;
				}
			}
		}else{
			$flag=false;
			if(!count($this->allowFields)){
				$flag=true;
			}
			foreach($data as $key=>$value){
				if(in_array($key,$this->allowFields) || $flag){
					$arr[$key]=':'.$key;
				}
			}
		}
		
		return $arr;
	}
	
}